<?php
// +----------------------------------------------------------------------
// | Longbing [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright Chengdu longbing Technology Co., Ltd.
// +----------------------------------------------------------------------
// | Website http://longbing.org/
// +----------------------------------------------------------------------
// | Sales manager: +86-13558882532 / +86-13330887474
// | Technical support: +86-15680635005
// | After-sale service: +86-17361005938
// +----------------------------------------------------------------------

namespace app\shortvideo\model;

use app\BaseModel;
use think\facade\Db;

class ShortvideoBrowse extends BaseModel{
    //定义表名
    protected $name = 'longbing_card_shortvideo_browse';


    /**
     * @author chenniang
     * @DataTime: 2020-05-19 09:50
     * @功能说明:获取统计数据
     */
    public function getCount($dis,$group='user_id'){

        $count = $this->where($dis)->group($group)->count();

        return $count;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-05-20 10:30
     * @功能说明:浏览人数统计
     */
    public function peopleList($dis){


        $data = $this->alias('a')
                ->join('longbing_card_user b','a.user_id = b.id')
                ->where($dis)
                ->field('b.nickName,b.avatarUrl,a.create_time,a.id,a.user_id')
                ->group('a.user_id')
                ->order('create_time desc,a.id desc')
                ->paginate(10)
                ->toArray();


        if(!empty($data['data'])){

            foreach ($data['data'] as $k=>&$v){
                //浏览时间
                $f_time = datachange($data['data'][$k],'create_time');

                $v['create_time'] = $f_time['radar_group'].$f_time['radar_time'];
                //次数
                $v['time']      = $this->alias('a')->where($dis)->where(['a.user_id'=>$v['user_id']])->count();
                //时长
                $v['time_long'] = $this->alias('a')->where($dis)->where(['a.user_id'=>$v['user_id']])->sum('browse_time');

                $v['time_long'] =  $v['time_long']<=0?1:$v['time_long'];

                $v['time_long'] = lbGetDatesss($v['time_long']);

                $v['avatarUrl']   = !empty($v['avatarUrl'])?$v['avatarUrl']:'https://lbqnyv2.migugu.com/defaultAvatar.png';

            }
//            exit;

        }
        return $data;
    }

    /**
     * @author chenniang
     * @DataTime: 2020-05-20 13:36
     * @功能说明:浏览详情
     */
    public function browseInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2020-05-20 13:27
     * @功能说明:增加浏览记录
     */
    public function browseAdd($data){

        $data['create_time'] = time();

        $data['browse_time'] = 0;

        $res = $this->insert($data,true);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-05-20 13:35
     * @功能说明:修改时长
     */
    public function updateBrowseTime($dis){

        $data = $this->browseInfo($dis);

        if(!empty($data)){

            $data['browse_time'] = time()-$data['create_time'];

            $this->where($dis)->update($data);
        }

        return true;
    }



}